/**
 * 包含所有接口请求函数模块
 * 每个函数的返回值都是promise
 */

import ajax from './axios'
import jsonp from 'jsonp'
import {message} from 'antd'

const BASE = 'http://120.55.193.14:5000'

/**
 * 登录模块
 */
// export function reqLogin (username,password) {
//   return ajax('/login',{username,password},'POST')
// }

/**
 * 优化登录模块
 */
export const reqLogin = (username,password) => ajax(BASE + '/login',{username,password},'POST')

/**
 * jsonp请求的接口请求函数
 */

export const reqWeather = (city) => {

  return new Promise((resolve,reject)=>{
    const url = `https://restapi.amap.com/v3/weather/weatherInfo?city=${city}&key=912f3a1404ef35b2e6a570ef9f217371`
    // 发送jsonp请求
    jsonp(url,{},(err,data)=>{
      console.log(err,data)
      if(!err && data.status === '1') {
        // 取出需要的数据
        const {weather} = data.lives[0]
        resolve({weather})
      } else {
        // 失败了
        message.error('获取天气失败',err)
      }
    })
  })
}

/**
 * 获取一级/二级分类接口
 */
export const reqCategorys = (parentId) => ajax(BASE + '/manage/category/list',{parentId})

/**
 * 添加分类接口
 */
 export const reqAddcategorys = ({categoryName,parentId}) => ajax(BASE + '/manage/category/add',{categoryName,parentId},'POST')

/**
 * 更新分类接口
 */
 export const reqUpdatecategorys = ({categoryId,categoryName}) => ajax(BASE + '/manage/category/update',{categoryId,categoryName},'POST')

 /**
  * 获取商品分页列表
  */

 export const reqProducts = (pageNum,pageSize) => ajax(BASE + '/manage/product/list',{pageNum,pageSize})

/**
 * 搜索商品分页列表(根据商品名称/商品描述)
 * searchType: 搜索的类型，productName/productDesc
 */
  export const reqSearchProducts = ({pageNum,pageSize,searchName,searchType}) => ajax(BASE + '/manage/product/search',{
    pageNum,
    pageSize,
    [searchType]: searchName,
  })

  /**
   * 获取一个分类
   */
  export const reqCategory = (categoryId) => ajax(BASE + '/manage/category/info', {categoryId})

  /**
   * 更新商品的状态(上架/下架)
   */
  export const reqUpdateStatus = (productId,status) => ajax(BASE + '/manage/product/updateStatus', {productId,status}, 'POST')

  /**
   * 添加/更新商品
   */
  export const reqAddOrUpdateProduce = (produce) => ajax(BASE+ '/manage/product/' + (produce._id ? 'update' : 'add'), produce,'POST')

  /**
   * 获取所有角色列表
   */
  export const reqRoles = () => ajax(BASE + '/manage/role/list')

  /**
   * 添加角色
   */
   export const reqAddRoles = (roleName) => ajax(BASE + '/manage/role/add', {roleName}, 'POST')

   /**
    * 更新角色权限
    */
    export const reqUpdateRoles = (role) => ajax(BASE + '/manage/role/update', role, 'POST')

    /**
    * 获取所有用户的列表
    */
    export const reqUsers = () => ajax(BASE + '/manage/user/list')

    /**
    * 删除某一个用户
    */
     export const reqDeleteUser = (userId) => ajax(BASE + '/manage/user/delete', {userId},'POST')

    /**
    * 添加/更新用户
    */
       export const reqAddOrUpdateUser = (user) => ajax(BASE + '/manage/user/' + (user._id ? 'update' : 'add'), user,'POST')


